Repository navigation
fix: resolve silent truncation on non-EILSEQ iconv errors - #762
Conversation
1. Root cause: In ChangeFileEncodingFormat(), the iconv do-while loop's else branch (for non-EILSEQ errors like EINVAL) performed a silent break, truncating all remaining content after the error byte 2. Fix: replace the break with the same recovery strategy used by the EILSEQ branch — skip the current input byte, append '?' to the output buffer, and continue the conversion loop instead of aborting 3. Impact: files containing invalid byte sequences (e.g. kern.log with 53263 lines showing only 1034) now load completely with '?' replacing invalid bytes instead of being silently truncated fix: 修复编码转换中非EILSEQ的iconv错误导致文件内容静默截断 1. 根因:ChangeFileEncodingFormat()的iconv循环中,else分支(处理EINVAL等 非EILSEQ错误)直接break,导致错误字节之后的所有内容被静默截断 2. 方案:将break替换为与EILSEQ分支相同的恢复策略——跳过当前输入字节、 在输出缓冲区追加'?'、继续循环而非中止转换 3. 影响:包含非法字节序列的文件(如kern.log 53263行仅显示1034行) 现在能完整加载,非法字节以'?'替代而非静默截断 Log: Fixed file content truncation when encountering invalid byte sequences during encoding conversion Influence: - Test opening files with invalid byte sequences to verify complete content display - Verify normal encoding files load without regression - Test UTF-8 to GB18030 and reverse conversion with mixed valid/invalid bytes Log: 修复包含非法字节序列的文件在编码转换时内容被静默截断的问题 Influence: - 测试打开含非法字节的文件,验证内容完整显示 - 验证正常编码文件加载无回归 - 测试UTF-8与GB18030互转场景下混合有效/非法字节的处理 PMS: BUG-384499
Reviewer's GuideImplementationReplace the non-EILSEQ error-path break with guarded replacement-character output and input advancement so conversion can continue.
TestsUpdate the truncated UTF-8 regression test to expect the invalid trailing byte to be replaced while preserving the converted prefix.
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've found 6 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="src/encodes/detectcode.cpp" line_range="999" />
<code_context>
} else {
- qCWarning(appLog) << "[DetectCode] iconv_open failed for conversion from" << fromCode << "to" << toCode;
- break;
+ qCWarning(appLog) << "[DetectCode] iconv conversion error for" << fromCode << "to" << toCode << "errno:" << errorNum;
+ if (outbytesleft == 0) {
+ qCWarning(appLog) << "Output buffer exhausted, breaking conversion";
</code_context>
<issue_to_address>
**Valid text is lost on E2BIG**
When iconv returns E2BIG because the remaining output space cannot fit the next character, the error handler treats E2BIG as malformed input, writes '?', and advances `inbuf` past valid data instead of retrying; callers receive corrupted conversion output as success.
Handle E2BIG by providing more output space and retrying without consuming the unconverted input.
Also at `src/encodes/detectcode.cpp:1009-1017`.
</issue_to_address>
### Comment 2
<location path="src/encodes/detectcode.cpp" line_range="1009" />
<code_context>
+ break;
+ }
+ outbytesleft -= 1;
+ *outbuf = '?';
+ outbuf += 1;
+ qCDebug(appLog) << "Appended replacement char '?' for non-EILSEQ error, remaining outbytesleft:" << outbytesleft;
</code_context>
<issue_to_address>
**Replacement output is malformed**
When a non-EILSEQ error occurs while converting to a multibyte target such as UTF-16 or UTF-32, `ChangeFileEncodingFormat` writes `?` as a single raw byte instead of encoding it in `toCode`, so the result is malformed and callers can save or receive undecodable output.
Encode the replacement character in `toCode` before writing it to the output buffer.
</issue_to_address>
### Comment 3
<location path="src/encodes/detectcode.cpp" line_range="1017-1018" />
<code_context>
+ inbytesleft = 0;
+ break;
+ }
+ inbuf += 1;
+ inbytesleft -= 1;
}
}
</code_context>
<issue_to_address>
**One truncated character becomes several**
When the input ends with a truncated multibyte character, the non-EILSEQ handler advances `inbuf` by one byte and retries, so each remaining byte of the incomplete character produces another `?` and the caller sees multiple replacements.
Treat the incomplete trailing sequence as one invalid character and emit only one replacement for it.
</issue_to_address>
### Comment 4
<location path="src/encodes/detectcode.cpp" line_range="1012-1015" />
<code_context>
+ *outbuf = '?';
+ outbuf += 1;
+ qCDebug(appLog) << "Appended replacement char '?' for non-EILSEQ error, remaining outbytesleft:" << outbytesleft;
+ if (inbytesleft <= 1) {
+ inbuf += inbytesleft;
+ inbytesleft = 0;
+ break;
+ }
+ inbuf += 1;
</code_context>
<issue_to_address>
**Large-file boundary characters are corrupted**
When a file larger than 40 MiB uses a non-UTF-8 encoding and a multibyte character straddles the 1 MiB head boundary, `ChangeFileEncodingFormat` converts the head and remainder independently; it replaces each byte of an incomplete multibyte character at the head’s end, while conversion of the remainder replaces its continuation bytes too, so that character appears as multiple `?` characters.
Preserve incomplete trailing bytes for conversion with the remainder, or use a streaming converter that carries state across the boundary.
</issue_to_address>
### Comment 5
<location path="tests/common2/test_detectcode.cpp" line_range="887" />
<code_context>
+ // Assert: 'a' 已转换,非法尾部字节替换为 '?'
EXPECT_TRUE(ok);
- EXPECT_EQ(out, QByteArrayLiteral("a"));
+ EXPECT_EQ(out, QByteArrayLiteral("a?"));
}
</code_context>
<issue_to_address>
**Regression test bypasses iconv**
When the test suite runs this updated truncated-UTF-8 test, the updated test converts UTF-8 to GB18030, but `ChangeFileEncodingFormat()` routes any conversion involving GB18030 to `convertEncodingTextCodec()` before reaching the changed iconv branch; the test therefore passes without exercising the fix and cannot catch a regression there.
Add a truncated-input test using an encoding pair that reaches the iconv branch and assert the replacement and converted output.
</issue_to_address>
### Comment 6
<location path="tests/common2/test_detectcode.cpp" line_range="882" />
<code_context>
{
- // Arrange: E6 EINVAL(不完整多字节序列在末尾)→ 保留已转换前缀
+ // Arrange: E6 EINVAL(不完整多字节序列在末尾)→ 'a' 已转换,尾部 C3 替换为 '?'
QByteArray in = QByteArrayLiteral("a") + QByteArray::fromHex("C3"), out;
// Act
const bool ok = DetectCode::ChangeFileEncodingFormat(in, out, QStringLiteral("UTF-8"), QStringLiteral("GB18030"));
</code_context>
<issue_to_address>
**Continuation is never verified**
When the conversion appends `?` for the incomplete trailing byte but incorrectly stops instead of resuming, the `ChangeEncoding_TruncatedUtf8_ReplacesInvalidByteAndContinues` test puts the invalid `C3` byte at the end of its input (line 882), so an implementation that appends `?` and then incorrectly stops still satisfies the `a?` assertion at line 887; the test does not verify that later content survives.
Add valid bytes after the incomplete sequence and assert that they appear after the replacement in the output.
</issue_to_address>| } else { | ||
| qCWarning(appLog) << "[DetectCode] iconv_open failed for conversion from" << fromCode << "to" << toCode; | ||
| break; | ||
| qCWarning(appLog) << "[DetectCode] iconv conversion error for" << fromCode << "to" << toCode << "errno:" << errorNum; |
There was a problem hiding this comment.
🟡 Medium · Valid text is lost on E2BIG
When iconv returns E2BIG because the remaining output space cannot fit the next character, the error handler treats E2BIG as malformed input, writes '?', and advances inbuf past valid data instead of retrying; callers receive corrupted conversion output as success.
Handle E2BIG by providing more output space and retrying without consuming the unconverted input.
Also at src/encodes/detectcode.cpp:1009-1017.
Prompt for AI agents
In `src/encodes/detectcode.cpp` at line 999:
**Valid text is lost on E2BIG**
When iconv returns E2BIG because the remaining output space cannot fit the next character, the error handler treats E2BIG as malformed input, writes '?', and advances `inbuf` past valid data instead of retrying; callers receive corrupted conversion output as success.
Handle E2BIG by providing more output space and retrying without consuming the unconverted input.
Also at `src/encodes/detectcode.cpp:1009-1017`.| break; | ||
| } | ||
| outbytesleft -= 1; | ||
| *outbuf = '?'; |
There was a problem hiding this comment.
🟡 Medium · Replacement output is malformed
When a non-EILSEQ error occurs while converting to a multibyte target such as UTF-16 or UTF-32, ChangeFileEncodingFormat writes ? as a single raw byte instead of encoding it in toCode, so the result is malformed and callers can save or receive undecodable output.
Encode the replacement character in toCode before writing it to the output buffer.
Prompt for AI agents
In `src/encodes/detectcode.cpp` at line 1009:
**Replacement output is malformed**
When a non-EILSEQ error occurs while converting to a multibyte target such as UTF-16 or UTF-32, `ChangeFileEncodingFormat` writes `?` as a single raw byte instead of encoding it in `toCode`, so the result is malformed and callers can save or receive undecodable output.
Encode the replacement character in `toCode` before writing it to the output buffer.| inbuf += 1; | ||
| inbytesleft -= 1; |
There was a problem hiding this comment.
🟡 Medium · One truncated character becomes several
When the input ends with a truncated multibyte character, the non-EILSEQ handler advances inbuf by one byte and retries, so each remaining byte of the incomplete character produces another ? and the caller sees multiple replacements.
Treat the incomplete trailing sequence as one invalid character and emit only one replacement for it.
Prompt for AI agents
In `src/encodes/detectcode.cpp` at lines 1017-1018:
**One truncated character becomes several**
When the input ends with a truncated multibyte character, the non-EILSEQ handler advances `inbuf` by one byte and retries, so each remaining byte of the incomplete character produces another `?` and the caller sees multiple replacements.
Treat the incomplete trailing sequence as one invalid character and emit only one replacement for it.| if (inbytesleft <= 1) { | ||
| inbuf += inbytesleft; | ||
| inbytesleft = 0; | ||
| break; |
There was a problem hiding this comment.
🟡 Medium · Large-file boundary characters are corrupted
When a file larger than 40 MiB uses a non-UTF-8 encoding and a multibyte character straddles the 1 MiB head boundary, ChangeFileEncodingFormat converts the head and remainder independently; it replaces each byte of an incomplete multibyte character at the head’s end, while conversion of the remainder replaces its continuation bytes too, so that character appears as multiple ? characters.
Preserve incomplete trailing bytes for conversion with the remainder, or use a streaming converter that carries state across the boundary.
Prompt for AI agents
In `src/encodes/detectcode.cpp` at lines 1012-1015:
**Large-file boundary characters are corrupted**
When a file larger than 40 MiB uses a non-UTF-8 encoding and a multibyte character straddles the 1 MiB head boundary, `ChangeFileEncodingFormat` converts the head and remainder independently; it replaces each byte of an incomplete multibyte character at the head’s end, while conversion of the remainder replaces its continuation bytes too, so that character appears as multiple `?` characters.
Preserve incomplete trailing bytes for conversion with the remainder, or use a streaming converter that carries state across the boundary.| // Assert: 'a' 已转换,非法尾部字节替换为 '?' | ||
| EXPECT_TRUE(ok); | ||
| EXPECT_EQ(out, QByteArrayLiteral("a")); | ||
| EXPECT_EQ(out, QByteArrayLiteral("a?")); |
There was a problem hiding this comment.
🟡 Medium · Regression test bypasses iconv
When the test suite runs this updated truncated-UTF-8 test, the updated test converts UTF-8 to GB18030, but ChangeFileEncodingFormat() routes any conversion involving GB18030 to convertEncodingTextCodec() before reaching the changed iconv branch; the test therefore passes without exercising the fix and cannot catch a regression there.
Add a truncated-input test using an encoding pair that reaches the iconv branch and assert the replacement and converted output.
Prompt for AI agents
In `tests/common2/test_detectcode.cpp` at line 887:
**Regression test bypasses iconv**
When the test suite runs this updated truncated-UTF-8 test, the updated test converts UTF-8 to GB18030, but `ChangeFileEncodingFormat()` routes any conversion involving GB18030 to `convertEncodingTextCodec()` before reaching the changed iconv branch; the test therefore passes without exercising the fix and cannot catch a regression there.
Add a truncated-input test using an encoding pair that reaches the iconv branch and assert the replacement and converted output.| { | ||
| // Arrange: E6 EINVAL(不完整多字节序列在末尾)→ 保留已转换前缀 | ||
| // Arrange: E6 EINVAL(不完整多字节序列在末尾)→ 'a' 已转换,尾部 C3 替换为 '?' | ||
| QByteArray in = QByteArrayLiteral("a") + QByteArray::fromHex("C3"), out; |
There was a problem hiding this comment.
🟡 Medium · Continuation is never verified
When the conversion appends ? for the incomplete trailing byte but incorrectly stops instead of resuming, the ChangeEncoding_TruncatedUtf8_ReplacesInvalidByteAndContinues test puts the invalid C3 byte at the end of its input (line 882), so an implementation that appends ? and then incorrectly stops still satisfies the a? assertion at line 887; the test does not verify that later content survives.
Add valid bytes after the incomplete sequence and assert that they appear after the replacement in the output.
Prompt for AI agents
In `tests/common2/test_detectcode.cpp` at line 882:
**Continuation is never verified**
When the conversion appends `?` for the incomplete trailing byte but incorrectly stops instead of resuming, the `ChangeEncoding_TruncatedUtf8_ReplacesInvalidByteAndContinues` test puts the invalid `C3` byte at the end of its input (line 882), so an implementation that appends `?` and then incorrectly stops still satisfies the `a?` assertion at line 887; the test does not verify that later content survives.
Add valid bytes after the incomplete sequence and assert that they appear after the replacement in the output.|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: lzwind, pengfeixx The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
fix: resolve silent truncation on non-EILSEQ iconv errors
else branch (for non-EILSEQ errors like EINVAL) performed a silent
break, truncating all remaining content after the error byte
EILSEQ branch — skip the current input byte, append '?' to the output
buffer, and continue the conversion loop instead of aborting
53263 lines showing only 1034) now load completely with '?' replacing
invalid bytes instead of being silently truncated
fix: 修复编码转换中非EILSEQ的iconv错误导致文件内容静默截断
非EILSEQ错误)直接break,导致错误字节之后的所有内容被静默截断
在输出缓冲区追加'?'、继续循环而非中止转换
现在能完整加载,非法字节以'?'替代而非静默截断
Log: Fixed file content truncation when encountering invalid byte sequences during encoding conversion
Influence:
Log: 修复包含非法字节序列的文件在编码转换时内容被静默截断的问题
Influence:
PMS: BUG-384499
Summary by Sourcery
Preserve file contents during encoding conversion when iconv encounters non-EILSEQ errors.
Bug Fixes:
Tests: